package edu.yctc.weblog.controller.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import edu.yctc.weblog.constant.ConstantHolder;
import edu.yctc.weblog.constant.RequestAttributes;
import edu.yctc.weblog.constant.TemplatePath;
import edu.yctc.weblog.controller.WeblogController;
import edu.yctc.weblog.dto.BlogDetailDTO;
import edu.yctc.weblog.entity.LinkDO;
import edu.yctc.weblog.entity.PageDO;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.enums.LinkType;
import edu.yctc.weblog.service.BlogService;
import edu.yctc.weblog.service.CommentService;
import edu.yctc.weblog.service.ConfigService;
import edu.yctc.weblog.service.LinkService;
import edu.yctc.weblog.service.TagService;

/**
 * 
 * @className WeblogControllerImpl
 * @description 主页相关控制层
 * @author zyj
 * @date 2020年6月22日
 *
 */
@Controller
public class WeblogControllerImpl implements WeblogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TagService tagService;

    @Autowired
    private LinkService linkService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private CommentService commentService;

    @Override
    @GetMapping({TemplatePath.S, TemplatePath.S + TemplatePath.INDEX, TemplatePath.S + TemplatePath.INDEX_HTML})
    public String showIndex(HttpServletRequest request) {
        return page(request, 1);
    }

    @Override
    @GetMapping(TemplatePath.S + TemplatePath.INDEX_PAGE + TemplatePath.S + "{pageNum}")
    public String page(HttpServletRequest request, @PathVariable("pageNum") int pageNum) {
        ResultDO<PageDO> resultDO = blogService.getBlogForIndexPage(pageNum);
        if (resultDO.isSuccess() == false) {
            return TemplatePath.ERROR_404;
        }
        request.setAttribute(RequestAttributes.BLOG_PAGE_RESULT, resultDO.getModule());
        request.setAttribute(RequestAttributes.NEW_BLOGS,
            blogService.getBlogListForIndexPage(ConstantHolder.TYPE_LASTEST_RELEASED).getModule());
        request.setAttribute(RequestAttributes.HOT_BLOGS,
            blogService.getBlogListForIndexPage(ConstantHolder.TYPE_MOST_CLICK).getModule());
        request.setAttribute(RequestAttributes.HOT_TAGS, tagService.getBlogTagCountForIndex().getModule());
        request.setAttribute(RequestAttributes.PAGE_NAME, RequestAttributes.VALUE_PAGE_NAME_INDEX);
        request.setAttribute(RequestAttributes.CONFIGURATIONS, configService.getAllConfigs().getModule());
        return TemplatePath.INDEX_BLOG + TemplatePath.S + ConstantHolder.THEME + TemplatePath.S + TemplatePath.INDEX;
    }

    @Override
    @GetMapping(TemplatePath.S + TemplatePath.INDEX_SEARCH + "/{keyword}")
    public String searchByKeword(HttpServletRequest request, @PathVariable("keyword") String keyword) {
        return searchByKewordAndPage(request, keyword, 1);
    }

    @Override
    @GetMapping(TemplatePath.S + TemplatePath.INDEX_SEARCH + "/{keyword}/{page}")
    public String searchByKewordAndPage(HttpServletRequest request, @PathVariable("keyword") String keyword,
        @PathVariable("page") Integer page) {
        PageDO pageDO = blogService.getBlogsPageBySearch(keyword, page).getModule();
        request.setAttribute(RequestAttributes.BLOG_PAGE_RESULT, pageDO);
        request.setAttribute(RequestAttributes.PAGE_NAME, RequestAttributes.VALUE_PAGE_NAME_SEARCH);
        request.setAttribute(RequestAttributes.KEYWORD, keyword);
        request.setAttribute(RequestAttributes.PAGE_URL, RequestAttributes.VALUE_SEARCH);
        request.setAttribute(RequestAttributes.NEW_BLOGS,
            blogService.getBlogListForIndexPage(ConstantHolder.TYPE_LASTEST_RELEASED).getModule());
        request.setAttribute(RequestAttributes.HOT_BLOGS,
            blogService.getBlogListForIndexPage(ConstantHolder.TYPE_MOST_CLICK).getModule());
        request.setAttribute(RequestAttributes.HOT_TAGS, tagService.getBlogTagCountForIndex().getModule());
        request.setAttribute(RequestAttributes.CONFIGURATIONS, configService.getAllConfigs().getModule());
        return TemplatePath.INDEX_BLOG + TemplatePath.S + ConstantHolder.THEME + TemplatePath.S
            + TemplatePath.LIST;
    }

    @Override
    @GetMapping(TemplatePath.S + TemplatePath.TAG + "/{tagName}")
    public String showBlogListByTagName(HttpServletRequest request, @PathVariable("tagName") String tagName) {
        return showBlogListByTagNameAndPage(request, tagName, 1);
    }

    @Override
    @GetMapping(TemplatePath.S + TemplatePath.TAG + "/{tagName}/{page}")
    public String showBlogListByTagNameAndPage(HttpServletRequest request, @PathVariable("tagName") String tagName,
        @PathVariable("page") Integer page) {
        PageDO pageDO = blogService.getBlogPageByTag(tagName, page).getModule();
        request.setAttribute(RequestAttributes.BLOG_PAGE_RESULT, pageDO);
        request.setAttribute(RequestAttributes.PAGE_NAME, RequestAttributes.VALUE_PAGE_NAME_TAG);
        request.setAttribute(RequestAttributes.PAGE_URL, RequestAttributes.VALUE_TAG);
        request.setAttribute(RequestAttributes.KEYWORD, tagName);
        request.setAttribute(RequestAttributes.NEW_BLOGS,
            blogService.getBlogListForIndexPage(ConstantHolder.TYPE_LASTEST_RELEASED).getModule());
        request.setAttribute(RequestAttributes.HOT_BLOGS,
            blogService.getBlogListForIndexPage(ConstantHolder.TYPE_MOST_CLICK).getModule());
        request.setAttribute(RequestAttributes.HOT_TAGS, tagService.getBlogTagCountForIndex().getModule());
        request.setAttribute(RequestAttributes.CONFIGURATIONS, configService.getAllConfigs().getModule());
        return TemplatePath.INDEX_BLOG + TemplatePath.S + ConstantHolder.THEME + TemplatePath.S + TemplatePath.LIST;
    }

    @Override
    @GetMapping({TemplatePath.S + TemplatePath.INDEX_BLOG + TemplatePath.S + "{blogId}",
        TemplatePath.S + TemplatePath.ARTICLE + TemplatePath.S + "{blogId}"})
    public String blogDetail(HttpServletRequest request, @PathVariable("blogId") Long blogId,
        @RequestParam(value = "commentPage", required = false, defaultValue = "1") Integer commentPage) {
        BlogDetailDTO blogDetailDTO = blogService.getBlogDetail(blogId).getModule();
        if (blogDetailDTO != null) {
            request.setAttribute(RequestAttributes.BLOG_DETAIL_VO, blogDetailDTO);
            request.setAttribute(RequestAttributes.COMMENT_PAGE_RESULT,
                commentService.getCommentPageByBlogIdAndPageNum(blogId, commentPage).getModule());
        }
        request.setAttribute(RequestAttributes.PAGE_NAME, RequestAttributes.VALUE_DETAIL);
        request.setAttribute(RequestAttributes.CONFIGURATIONS, configService.getAllConfigs().getModule());
        return TemplatePath.INDEX_BLOG + TemplatePath.S + ConstantHolder.THEME + TemplatePath.S + TemplatePath.DETAIL;
    }

    @Override
    @GetMapping(TemplatePath.S + TemplatePath.LINK)
    public String showLink(HttpServletRequest request) {
        request.setAttribute(RequestAttributes.PAGE_NAME, RequestAttributes.VALUE_PAGE_NAME_LINKS);
        Map<Integer, List<LinkDO>> linkMap = linkService.getLinksForLinkPage().getModule();
        if (linkMap != null) {
            if (linkMap.containsKey(LinkType.FRIENDLY)) {
                request.setAttribute(RequestAttributes.LINK_FAVORITE, linkMap.get(LinkType.FRIENDLY));
            }
            if (linkMap.containsKey(LinkType.RECOMMENDED)) {
                request.setAttribute(RequestAttributes.LINK_COMMEND, linkMap.get(LinkType.RECOMMENDED));
            }
            if (linkMap.containsKey(LinkType.PERSONAL)) {
                request.setAttribute(RequestAttributes.LINK_PERSONAL, linkMap.get(LinkType.PERSONAL));
            }
        }
        request.setAttribute(RequestAttributes.CONFIGURATIONS, configService.getAllConfigs().getModule());
        return TemplatePath.INDEX_BLOG + TemplatePath.S + ConstantHolder.THEME + TemplatePath.S + TemplatePath.LINK;
    }

    @Override
    @GetMapping(TemplatePath.S + "{subUrl}")
    public String showSubUrlBlog(HttpServletRequest request, @PathVariable("subUrl") String subUrl) {
        BlogDetailDTO blogDetailDTO = blogService.getBlogDetailBySubUrl(subUrl).getModule();
        if (blogDetailDTO != null) {
            request.setAttribute(RequestAttributes.BLOG_DETAIL_VO, blogDetailDTO);
            request.setAttribute(RequestAttributes.PAGE_NAME, subUrl);
            request.setAttribute(RequestAttributes.CONFIGURATIONS, configService.getAllConfigs().getModule());
            request.setAttribute(RequestAttributes.COMMENT_PAGE_RESULT,
                commentService.getCommentPageByBlogIdAndPageNum(blogDetailDTO.getBlogId(), 1).getModule());
            return TemplatePath.INDEX_BLOG + TemplatePath.S + ConstantHolder.THEME + TemplatePath.S
                + TemplatePath.DETAIL;
        } else {
            return TemplatePath.ERROR_400;
        }
    }

    @Override
    @GetMapping(TemplatePath.INDEX_CATEGORY + TemplatePath.S + "{categoryName}")
    public String showCategory(HttpServletRequest request, @PathVariable("categoryName") String categoryName) {
        return category(request, categoryName, 1);
    }

    @Override
    @GetMapping(TemplatePath.INDEX_CATEGORY + TemplatePath.S + "{categoryName}" + TemplatePath.S + "{page}")
    public String category(HttpServletRequest request, @PathVariable("categoryName") String categoryName,
        @PathVariable("page") Integer page) {
        ResultDO<PageDO> resultDO = blogService.getBlogsPageByCategory(categoryName, page);
        request.setAttribute(RequestAttributes.BLOG_PAGE_RESULT, resultDO.getModule());
        request.setAttribute(RequestAttributes.PAGE_NAME, RequestAttributes.VALUE_PAGE_NAME_CATEGORY);
        request.setAttribute(RequestAttributes.PAGE_URL, RequestAttributes.VALUE_CATEGORY);
        request.setAttribute(RequestAttributes.KEYWORD, categoryName);
        request.setAttribute(RequestAttributes.NEW_BLOGS,
            blogService.getBlogListForIndexPage(ConstantHolder.TYPE_LASTEST_RELEASED).getModule());
        request.setAttribute(RequestAttributes.HOT_BLOGS,
            blogService.getBlogListForIndexPage(ConstantHolder.TYPE_MOST_CLICK).getModule());
        request.setAttribute(RequestAttributes.HOT_TAGS, tagService.getBlogTagCountForIndex().getModule());
        request.setAttribute(RequestAttributes.CONFIGURATIONS, configService.getAllConfigs().getModule());
        return TemplatePath.INDEX_BLOG + TemplatePath.S + ConstantHolder.THEME + TemplatePath.S + TemplatePath.LIST;
    }

}
